<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;
use util\Tree;

/**
 * 服务分类管理
 * @package app\erp\model
 */
class ErpServiceClass extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_service_class';
	
	/**
     * 获取树形节点
     * @param int $id 需要隐藏的节点id
     * @param string $default 默认第一个节点项，默认为“顶级节点”，如果为false则不显示，也可传入其他名称
     * @param string $module 模型名
     * @author shiqiang <976162041@qq.com>	
     * @return mixed
     */
    public static function getMenuTree($id = 0, $default = '')
    {
        $result[0] = '顶级节点';
        $where = [
            ['status', 'egt', 0]
        ];
		
		// 排除指定节点及其子节点
        if ($id !== 0) {
            $hide_ids = array_merge([$id], self::getChildsId($id));
            $where[]  = ['id', 'not in', $hide_ids];
        }

        // 获取节点
        $menus = Tree::toList(self::where($where)->order('sort asc,pid,id')->column('id,pid,title'));
        foreach ($menus as $menu) {
            $result[$menu['id']] = $menu['title_display'];
        }

        // 设置默认节点项标题
        if ($default != '') {
            $result[0] = $default;
        }

        // 隐藏默认节点项
        if ($default === false) {
            unset($result[0]);
        }

        return $result;
    }
	
	/**
     * 获取所有子节点id
     * @param int $pid 父级id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public static function getChildsId($pid = 0)
    {
        $ids = self::where('pid', $pid)->column('id');
        foreach ($ids as $value) {
            $ids = array_merge($ids, self::getChildsId($value));
        }
        return $ids;
    }
	
	/**
     * 获取所有子节点id和path
     * @param int $pid 父级id
     * @author shiqiang
     * @return array
     */
    public static function getChildsPath($pid = 0)
    {
        $ids = self::where('pid', $pid) -> column('id,path');
        foreach ($ids as $key => $value) {
            $ids = $ids + self::getChildsPath($key);
        }
        return $ids;
    }
	
	/**
     * 获取所有分类
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getClass(){
		$where = [
            ['status', 'egt', 0]
        ];
		$menus = Tree::toList(self::where($where)->order('sort,pid,id')->column('id,pid,title,sort,code,class_img,create_time'));
		return $menus;
	}
	
	/**
     * 获取所有父节点id
     * @param int $id 节点id
     * @author shiqiang
     * @return array
     */
    public static function getParentsId($id = 0)
    {
        $pid  = self::where('id', $id)->value('pid');
        $pids = [];
        if ($pid != 0) {
            $pids[] = $pid;
            $pids = array_merge($pids, self::getParentsId($pid));
        }
		sort($pids);
        return $pids;
    }
	
    /**
     * 添加数据
     * @param int 	 $pid 		上级id
     * @param string $title 	分类标题
     * @param int 	 $sort 		排序
     * @param string $code 		编码
     * @param string $class_img 		分类图片
     * @author shiqiang
     * @return array_out(code, message, (int)id)
     */
    public function addData($pid, $title, $sort, $code, $class_img){
        $data['pid']            = $pid;
        $data['title']         	= $title;
        $data['sort']         	= $sort;
        $data['code']         	= $code;
        $data['class_img']        = $class_img;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success' , $this -> id);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	
	/**
     * 编辑数据
     * @param int $id 		
     * @param int $pid 		上级id
     * @param string $title 分类标题
     * @param int $sort 	排序
     * @param string $code 	编码
     * @param string $class_img 	分类图片
     * @author shiqiang
     * @return array_out(code, message, (int)id)
     */
	public function editData($id, $pid, $title, $sort, $code, $class_img){
		$map = [];
		$map['id'] = $id;
		
		$data = [];
        $data['title']   	= $title;
        $data['sort']    	= $sort;
        $data['code']    	= $code;
        $data['class_img']  = $class_img;
		
		$res = $this ->update($data, $map);
		if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 更新分类path
     * @param int $id 		
     * @param string $path 	分类路径
     * @author shiqiang
     * @return array_out(code, message, (int)id)
     */
	public function updateDataByPath($id, $path){
		$map = [];
		$map['id'] = $id;
		
		$data = [];
		$data['path']     	= $path;
		
		$res = $this ->update($data, $map);
		if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 删除数据
     * @param array $id
     * @author shiqiang
     * @return bool
     */
    public function deleteData($class_id){
        $res = self::destroy($class_id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
